package pl.edu.pw.MidEmu.Common.Models;

import java.net.Inet4Address;
import java.net.NetworkInterface;

/**
 * Model class representing Ethernet Interface
 * @author Mateusz Starodab
 *
 */
public class EthernetInterfaceModel
{
	private NetworkInterface _interface;
	private Inet4Address _address;
	private boolean isFree = true;
	
	/**
	 * Model constructor
	 * @param ethernetInterface Ethernet interface
	 * @param inetAddress Address assigned to interface
	 */
	public EthernetInterfaceModel(NetworkInterface ethernetInterface, Inet4Address inetAddress)
	{
		_interface = ethernetInterface;
		_address = inetAddress;
	}

	/**
	 * Gets Ethernet IP address
	 * @return IP address
	 */
	public String getIpAddress()
	{
		return _address.toString().replace("/", "");
	}

	/**
	 * Gets interface name
	 * @return Interface name
	 */
	public String getName()
	{
		return _interface.getDisplayName() + "_" + getIpAddress();
	}

	/**
	 * Checks if interface is unused by MidEmu emulator
	 * @return True - if interface is free
	 */
	public boolean isFree()
	{
		return isFree;
	}

	/**
	 * Stest if interface is free
	 * @param isFree Free flag
	 */
	public void setFree(boolean isFree)
	{
		this.isFree = isFree;
	}

	@Override
	/**
	 * Returns string representation of this ethernet model
	 */
	public String toString()
	{
		return _interface.getDisplayName() + " - " + getIpAddress();
	}

	/**
	 * Gets interface display name
	 * @return Display name
	 */
	public String getDisplayName()
	{
		return _interface.getDisplayName();
	}

//	public String getMacAddress()
//	{
//		byte[] macAddressBytes;
//		String macAddress = "00:00:00:00:00:00";
//		try
//		{
//			macAddressBytes = _interface.getHardwareAddress();
//			macAddress = String.format("%1$02x:%2$02x:%3$02x:%4$02x:%5$02x:%6$02x", macAddressBytes[0], macAddressBytes[1], macAddressBytes[2], macAddressBytes[3], macAddressBytes[4], macAddressBytes[5]).toUpperCase();
//
//		}
//		catch (SocketException e)
//		{
//			e.printStackTrace();
//		}
//		return macAddress;
//	}
}
